<?php

namespace app\Http\Controllers\admins;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class Trash extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            // $page = $request->page ?? 1;
            $limit = $request->limit ?? 10;
            $db = DB::table('article');
            // 搜索
            if (isset($request->search) && $request->search) {
                foreach ((array)$request->search as $k => $v) {
                    if ($v) $db = $db->where($k, 'like', "%{$v}%");
                }
            }
            $db = $db->where('is_del', 1)
                ->orderBy('id', 'desc');
            $data['count'] = $db->count();
            // 分页
            $db = $db->paginate($limit)->items();
            if ($data['count']) {
                $data['code'] = 0;
            } else {
                $data['code'] = 1;
                $data['msg'] = '暂无记录';
            }
            // 获取分类
            $cats = DB::table('cat')->format('id');
            $tags = DB::table('tag')->format('id');
            // 格式化数据
            $data['data'] = array_map(function ($item) use ($cats, $tags) {
                $item->thumb = $item->thumb ? '<img src="' . $item->thumb . '" style="width:50px;height:30px;" />' : '';
                $item->excerpt = mb_strimwidth(strip_tags($item->excerpt), 0, 60, '...', 'utf-8');
                $item->cat_id = $cats[$item->cat_id]->name;
                $item->tags = $item->tags ? implode(', ', array_reduce(array_map('trim', json_decode($item->tags)), function ($res, $next) use ($tags) {
                    $res[] = $tags[$next]->name;
                    return $res;
                }, [])) : '';
                $item->status = $item->status ? '禁用' : '启用';
                return $item;
            }, $db);
            return json_encode($data);
        }
        $form = [
            ['label' => '标题', 'name' => 'title', 'layui' => 'inline', 'placeholder' => '请输入'],
            ['label' => '摘要', 'name' => 'excerpt', 'layui' => 'inline', 'placeholder' => '请输入'],
            ['label' => '内容', 'name' => 'content', 'layui' => 'inline', 'placeholder' => '请输入'],
            ['type' => 'submit', 'filter' => 'search', 'layui' => 'inline', 'value' => '<i class="layui-icon layui-icon-search"></i>']
        ];
        $data['search'] = $this->build_form($form);
        $data['title'] = '文章列表';
        $data['prefix'] = '/admins/trash';
        return view('/admins/trash/index', $data);
    }

    public function restore(Request $request) {
        $id = (array)$request->id;
        if (!$id) return $this->errMsg(1, '请选择数据');
        $res = DB::table('article')->whereIn('id', $id)->update(['is_del' => 0]);
        if (!$res) return $this->errMsg(1, '还原失败');
        return $this->errMsg(0, '还原成功');
    }

    public function del(Request $request)
    {
        $id = (array)$request->id;
        if (!$id) return $this->errMsg(1, '请选择数据');
        // 删除评论
        DB::table('comment')->whereIn('post_id', $id)->delete();
        // 删除文章
        $res = DB::table('article')->whereIn('id', $id)->delete();
        if (!$res) return $this->errMsg(1, '删除失败');
        return $this->errMsg(0, '删除成功');
    }
}
